//先进的先出
public class MyQueue {
    //双链表
    static class ListNode {
        public ListNode prev;
        public ListNode next;
        public int val;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;

    //尾插
    public boolean offer(int val) {
        ListNode node = new ListNode(val);
        if (head == null) {
            head = last = node;
            return true;
        }
        last.next = node;
        node.prev = last;
        last = node;
        return true;
    }

    //头删
    public int poll() {
        if (isEmpty()) {
            return -1;
        }
        int val = head.val;
        head = head.next;
        //临界条件
        if (head != null) {
            head.prev = null;
        }
        return val;
    }

    public int peek() {
        if (isEmpty()) {
            return -1;
        }
        return head.val;
    }

    public int size() {
        int size = 0;
        ListNode cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

}
